package com.code.wflow.controller;

import com.code.wflow.constants.ResultCode;
import com.code.wflow.constants.WorkflowStatus;
import com.code.wflow.model.FormItem;
import com.code.wflow.model.Workflow;
import com.code.wflow.service.FormItemService;
import com.code.wflow.service.WorkflowService;
import com.code.wflow.utils.BeanCopierUtils;
import com.code.wflow.wrapper.Result;
import com.code.wflow.wrapper.request.WorkflowIdRequest;
import com.code.wflow.wrapper.response.FormItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流程相关
 *
 * @author ggh
 * @date 2022/10/26
 */
@RequestMapping(value = "/form")
@RestController
public class FormController {

    @Autowired
    private WorkflowService workflowService;
    @Autowired
    private FormItemService formItemService;

    /**
     * 查询表单信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/formList")
    public Result<String> formList(@RequestBody WorkflowIdRequest request) {
        if (request == null || request.getWorkflowId() == null || request.getWorkflowId() == null) {
            return Result.failure(ResultCode.PARAM_LACK);
        }
        try {
            Workflow workflow = workflowService.queryById(request.getWorkflowId());
            if (workflow == null || WorkflowStatus.DISABLE.getVal().equals(workflow.getStatus())) {
                return Result.failure(ResultCode.WORKFLOW_NOT_EXIST);
            }

            List<FormItem> formItemList = formItemService.queryByWorkflowId(request.getWorkflowId());
            List<FormItemVO> formItemVOList = BeanCopierUtils.copyList(formItemList, FormItemVO.class);
            return Result.ok(formItemVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.failure(ResultCode.FORM_QUERY_ERROR);
    }
}
